package com.bookstore.controller;

import com.bookstore.bean.LoginForm;
import com.bookstore.bean.User;
import com.bookstore.service.serviceImpl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/system")
public class SystemController {

    //注入业务对象
    @Autowired
    private UserService userService;
//    @Autowired
//    private TeacherService teacherService;
//    @Autowired
//    private StudentService studentService;

    //存储返回给页面的对象数据
    private Map<String, Object> result = new HashMap<>();

    /**
     * @description: 跳转到用户登录页面
     * @param: no
     * @date: 2019-06-09 6:34 PM
     * @return: java.lang.String
     */
    @GetMapping("/goLogin")
    public String goLogin() {
        return "system/login";
    }




    /**
     * @description: 验证用户登录信息
     * @param: loginForm
     * @param: request

     */
    @PostMapping("login")
    @ResponseBody
    public Map<String, Object> login(LoginForm loginForm, HttpServletRequest request) {


        //效验用户登录信息
        switch (loginForm.getUserType()) {
//
            //用户
            case 1:
                try {
                    User user = userService.login(loginForm);
                    if (user != null) {
                        HttpSession session = request.getSession();
                        session.setAttribute("userInfo", user);
                        session.setAttribute("userType", loginForm.getUserType());
                        result.put("success", true);
                        return result;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success", false);
                    result.put("msg", "登录失败! 服务器端发生异常!");
                    return result;
                }
                break;
            //管理员身份
//            case 2:
//                try {
//                    Admin admin = adminService.login(loginForm);//验证账户和密码是否存在
//                    if (admin != null) {
//                        HttpSession session = request.getSession(); //将用户信息存储到Session
//                        session.setAttribute("userInfo", admin);
//                        session.setAttribute("userType", loginForm.getUserType());
//                        result.put("success", true);
//                        return result;
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    result.put("success", false);
//                    result.put("msg", "登录失败! 服务器端发生异常!");
//                    return result;
//                }
//                break;
            //卖家身份
//            case 3:
//                try {
//                    Teacher teacher = teacherService.login(loginForm);
//                    if (teacher != null) {
//                        HttpSession session = request.getSession();
//                        session.setAttribute("userInfo", teacher);
//                        session.setAttribute("userType", loginForm.getUserType());
//                        result.put("success", true);
//                        return result;
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    result.put("success", false);
//                    result.put("msg", "登录失败! 服务器端发生异常!");
//                    return result;
//                }
//                break;
        }
        //登录失败
        result.put("success", false);
        result.put("msg", "用户名或密码错误!");
        return result;
    }

    /**
     * @description: 跳转到系统主页面
     * @param: no

     */
    @GetMapping("/goSystemMainView")
    public String goSystemMainView() {
        return "system/main";
    }


    /**
     * @description: 注销用户信息
     * @param: request
     * @date: 2019-06-11 6:40 PM
     * @return: void
     */
    @GetMapping("/loginOut")
    public void loginOut(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute("userInfo");
        request.getSession().removeAttribute("userType");

        //注销后重定向到登录页面
        try {
            response.sendRedirect("../index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @GetMapping ("/reg")
    public String reg(HttpServletResponse response) throws IOException {
        //response.sendRedirect("../reg");
      return "/system/reg";
    }
    @PostMapping("register")
    @ResponseBody
    public Map<String, Object> register(LoginForm loginForm, HttpServletRequest request) {


        //效验用户注册信息
        switch (loginForm.getUserType()) {
//
            //用户
            case 1:
                try {
                    userService.addUser(loginForm);
                    result.put("success", true);
                    result.put("msg", "注册成功!");
                    return result;
                } catch (Exception e) {
                    e.printStackTrace();
                    result.put("success", false);
                    result.put("msg", "注册失败! 服务器端发生异常!");
                    return result;
                }
                //卖家
//            case 3:
//                try {
//                    Teacher teacher = teacherService.login(loginForm);
//                    if (teacher != null) {
//                        HttpSession session = request.getSession();
//                        session.setAttribute("userInfo", teacher);
//                        session.setAttribute("userType", loginForm.getUserType());
//                        result.put("success", true);
//                        return result;
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                    result.put("success", false);
//                    result.put("msg", "登录失败! 服务器端发生异常!");
//                    return result;
//                }
//                break;
        }
        //注册失败
        result.put("success", false);
        result.put("msg", "用户名或密码错误!");
        return result;
    }
}
